package com.example.aiserver.controller;

import com.example.aiserver.common.ApiResponse;
import com.example.aiserver.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @PostMapping("/login")
    public ResponseEntity<ApiResponse<?>> login(@RequestParam String username, @RequestParam String password) {
        String result = userService.loginWithDetails(username, password);

        switch (result) {
            case "USER_NOT_FOUND":
                return ResponseEntity.ok(ApiResponse.failure(404, "用户不存在"));
            case "PASSWORD_INCORRECT":
                return ResponseEntity.ok(ApiResponse.failure(401, "密码错误"));
            case "LOGIN_SUCCESS":
                return ResponseEntity.ok(ApiResponse.success("登录成功"));
            default:
                return ResponseEntity.ok(ApiResponse.failure(500, "未知错误"));
        }
    }

    @PostMapping("/register")
    public ResponseEntity<ApiResponse<?>> register(@RequestParam String username, @RequestParam String password) {
        String result = userService.registerWithDetails(username, password);

        switch (result) {
            case "USER_EXISTS":
                return ResponseEntity.ok(ApiResponse.failure(409, "用户名已存在"));
            case "REGISTER_SUCCESS":
                return ResponseEntity.ok(ApiResponse.success("注册成功"));
            default:
                return ResponseEntity.ok(ApiResponse.failure(500, "未知错误"));
        }
    }
}
